﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VSSTQH.Models;
using VSSTQH.DataAccess;

namespace VSSTQH
{
    public partial class FrmPOList : Form
    {

        #region Ctors
        private FrmPOList()
        {
            InitializeComponent();

            Initialize();

        }
        #endregion

        #region Singleton Members
        private static volatile FrmPOList instance;
        private static object syncRoot = new Object();

        public static FrmPOList Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FrmPOList();
                }

                return instance;
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            instance = null;
            base.OnClosed(e);
        }
        #endregion

        #region Method Members

        /// <summary>
        /// Initialize methods of the application
        /// </summary>
        void Initialize()
        {
            IPODAO dao = new PODAO();
            IList<ProductModel> products = dao.GetProducts();
            if (products != null)
            {
                products.Insert(0, new ProductModel() { ProductID = "Chọn hàng hóa ..." });
                this.cboProduct.DataSource = products;
                this.cboProduct.ValueMember = "ProductName";
                this.cboProduct.DisplayMember = "ProductID";

            }


            IList<VendorModel> vendors = dao.GetVendors();
            if (vendors != null)
            {
                vendors.Insert(0, new VendorModel() { VendorID = "Chọn khách hàng ..." });
                this.cboVendor.DataSource = vendors;
                this.cboVendor.ValueMember = "VendorName";
                this.cboVendor.DisplayMember = "VendorID";
               
            }


            this.btnReset.Click += new EventHandler(delegate { ClickButtonReset(); });
            this.btnSearch.Click += new EventHandler(delegate { ClickButtonSearch(); });
            this.btnEdit.Click += new EventHandler(delegate { ClickButtonEdit(); });
            this.btnDelete.Click += new EventHandler(delegate { ClickButtonDelete(); });
            this.btnClose.Click += new EventHandler(delegate { this.Close(); });
            this.gridEXPo.FormattingRow += new Janus.Windows.GridEX.RowLoadEventHandler(gridEXPo_FormattingRow);

            // Default value
            this.gridEXPo.BuiltInTexts[Janus.Windows.GridEX.GridEXBuiltInText.GroupByBoxInfo] = String.Empty;

            // Show all customers
            this.ClickButtonSearch();

            if (Common.UserLogin.GroupID != 1)
            {
                this.btnDelete.Visible = false;
            }
        }

        void gridEXPo_FormattingRow(object sender, Janus.Windows.GridEX.RowLoadEventArgs e)
        {
            if (e.Row.Cells["POType"].Value == null) return;
            if (e.Row.Cells["POType"].Value.Equals(1))
            {
                e.Row.Cells["POType"].Text = "Nhập hàng";
            }
            else
            {
                e.Row.Cells["POType"].Text = "Xuất hàng";
            }

        }

        #endregion

        #region Action Members

        /// <summary>
        /// Click button reset
        /// </summary>
        private void ClickButtonReset()
        {
            this.cboVendor.SelectedIndex = -1;
            this.cboProduct.SelectedIndex = -1;
            this.dateTimePicker1.Value = DateTime.Today;
            this.dateTimePicker2.Value = DateTime.Today;

            this.ClickButtonSearch();

            this.cboVendor.Focus();
        }

        /// <summary>
        /// Click button search
        /// </summary>
        private void ClickButtonSearch()
        {
            IPODAO dao = new PODAO();
            string vendorID = String.Empty;
            string productID = String.Empty;
            string poID = txtPOID.Text;
            string vehicleID = txtVehicleID.Text;

            if (cboVendor.Value != null)
            {
                vendorID = cboVendor.Text;
            }

            if (cboProduct.Value != null)
            {
                productID = cboProduct.Text;
            }

            IList<POModel> poList = dao.Search(this.dateTimePicker1.Value, this.dateTimePicker2.Value,
                vendorID, productID, cboPoType.SelectedIndex, vehicleID, poID);

            if (poList == null || poList.Count == 0)
                this.gridEXPo.BuiltInTexts[Janus.Windows.GridEX.GridEXBuiltInText.GroupByBoxInfo] = "Không tìm thấy thông tin."; // Bạn phải nhập ít nhất 1 tiêu chí tìm kiếm.";
            else
                this.gridEXPo.BuiltInTexts[Janus.Windows.GridEX.GridEXBuiltInText.GroupByBoxInfo] = "Chọn cột cần group.";

            this.gridEXPo.DataSource = poList;
        }

        /// <summary>
        /// Click button edit
        /// </summary>
        private void ClickButtonEdit()
        {
            if (this.gridEXPo.CurrentRow == null || this.gridEXPo.CurrentRow.DataRow == null)
            {
                Common.ShowAlert("Vui lòng chọn dòng cần chỉnh sửa.");
                return;
            }

            // Edit employee information
            FrmEditPO instance = new FrmEditPO();
            instance.CurrentPO = this.gridEXPo.CurrentRow.DataRow as POModel;
            DialogResult result = instance.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Refresh data
                this.gridEXPo.Refetch();
            }
        }

        /// <summary>
        /// Click button delete
        /// </summary>
        private void ClickButtonDelete()
        {
            if (this.gridEXPo.CurrentRow == null || this.gridEXPo.CurrentRow.DataRow == null)
            {
                Common.ShowAlert("Bạn chưa chọn PO cần xóa");
                return;
            }

            POModel model = this.gridEXPo.CurrentRow.DataRow as POModel;
            bool? result = Common.ShowQuestion("Bạn có chắc sẽ xóa PO này?");
            if (result.HasValue && result.Value)
            {
                IPODAO dao = new PODAO();
                if (dao.Delete(model))
                {
                    this.gridEXPo.CurrentRow.Delete();
                    this.gridEXPo.Refetch();
                }
                else
                {
                    Common.ShowAlert("Xóa PO không thành công!");
                }
            }
        }

        #endregion

    }
}
